﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EShop.BussinessObjects;
using EShop.Common;
using EShop.DataAccess;
using Mabna.Total.BOHelper;
using System.Net.Mail;
using System.Configuration;
using System.Net;


public partial class ForgetPassword : BaseThemePage, ICallbackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region CallBack Scripts

        string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "forgetpassReceive", "context", true);

        string callbackScript;
        callbackScript = "function forgetpassCallServer(arg, context)" +
            "{" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "forgetpassCallServer", callbackScript, true);

        #endregion
    }


    #region ICallbackEventHandler Members
    string strResult="";
    public string GetCallbackResult()
    {
        return strResult;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        List<Object> ClientData = (List<Object>)ObjectSerializer.FromJson(eventArgument, typeof(List<Object>));

        string Command = ClientData[0].ToString();
        List<Object> ServerData = new List<object>();

        if (Command == "SendEmail")
        {
            int NotEffective=(int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
            User user_Filter = new User();
            user_Filter.ID = NotEffective;
            user_Filter.Contact = new Contact();
            user_Filter.Contact.ID = NotEffective;
            user_Filter.LoginID = ClientData[1].ToString();
            List<User> userList = CDAUser.Select(user_Filter);

            if (userList.Count > 0)
            {
                ServerData.Add("SendEmail");
                try
                {
                    SendEmail(userList.First());
                    ServerData.Add("Success");
                }
                catch (Exception ex)
                {
                    ServerData.Add("Fail");
                    ServerData.Add(ex.Message);
                }
                strResult = ServerData.ToJSON();
            }
            else
            {
                ServerData.Add("Fail");
                ServerData.Add("Benutzername ist nicht wahr");
                strResult = ServerData.ToJSON();
            }
        }
    }

    #endregion


    private void SendEmail(User user)
    {
        MailMessage mailmessage = new MailMessage();
        //mailmessage.From = new MailAddress("test.mabna.net@gmail.com");
        mailmessage.To.Add(new MailAddress(user.LoginID));
        mailmessage.Subject = "Recovery Password";
        mailmessage.IsBodyHtml=true;
        mailmessage.Body = "<html xmlns=\"http://www.w3.org/1999/xhtml\">" +
                         "<head></head>" +
                            "<body>" +
                                "<table style=\"width:100%;\">" +
                                    "<tr>" +
                                        "<td colspan=\"2\">" +
                                        "</td>" +
                                    "</tr>" +
                                    "<tr>" +
                                        "<td>" +
                                            "UserName</td>" +
                                        "<td>" +
                                        user.LoginID+
                                        "</td>" +
                                     "</tr>" +
                                     "<tr>" +
                                        "<td>" +
                                             "Password</td>" +
                                         "<td>" +
                                         user.Password+
                                         "</td>" +
                                     "</tr>" +
                                    "</table>" +
                                "</body></html>";

        //SmtpClient smtp = new SmtpClient("smtp.gmail.com",25);
        SmtpClient smtp = new SmtpClient();
        //NetworkCredential cred = new NetworkCredential("test.mabna.net@gmail.com", "mabnamabnamabna");
        //smtp.Credentials = cred;
        
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mailmessage);
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
}
